package com.lzyyj.gmis.base.model;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ThemeStyle {

	private static Log log = LogFactory.getLog(ThemeStyle.class);
	
	private ThemeType themeType;
	
	public ThemeType getThemeType() {
		return themeType;
	}

	public void setThemeType(ThemeType themeType) {
		this.themeType = themeType;
	}

	private String name;
	
	public static String icon = "ThemeThumb.gif";
	
	private String[] excludeCss = new String[]{"common","datepicker","jquery"};
	
	public boolean hasIcon;
	
	private Map<String, String> cssMap = new HashMap<String, String>();
	
	private String mainCss;

	private File mainCssFile = null;

	private boolean valid = false;
	
	private ThemeStyle(ThemeType themeType, File themeStyleFile) {
		this.themeType = themeType;
		this.name = themeStyleFile.getName();;
		findCSS("", themeStyleFile, 3);

	}

	public static ThemeStyle getInstance(ThemeType themeType, File themeStyleDir) {
		ThemeStyle themeStyle = null;
		
		if (themeStyleDir!=null&&themeStyleDir.exists()&&themeStyleDir.isDirectory()){
			boolean isStyleDir = false;
			
			// 当前目录及下一级目录下检索css样式文件
			for (File file:themeStyleDir.listFiles()){
				if(file.isFile()&&file.getName().toLowerCase().endsWith(".css")){
					isStyleDir = true;
					break;
				}else if(file.isDirectory()){
					Boolean haveCSS = false;
					for (File file1:themeStyleDir.listFiles()){
						if(file1.isFile()&&file1.getName().toLowerCase().endsWith(".css")){
							haveCSS = true;
							break;
						}
					}
					if(haveCSS){
						isStyleDir = true;
						break;
					}
				}
			}
			if(isStyleDir){
				themeStyle = new ThemeStyle(themeType, themeStyleDir);
			}
		}
		
		return themeStyle;
	}

	/**
	 * 找出Style目录下的css样式文件，并将最大的文件设置为默认样式文件
	 * @param basePath 基本相对路径
	 * @param dirFile 目录文件
	 * @param depth 遍历深度 (-1 为不限)
	 */
	private void findCSS(String basePath, File dirFile, int depth) {
		if (depth==-1||depth>0){
			if (dirFile.isDirectory()){
				for (File file:dirFile.listFiles()){
					if (file.isDirectory()){
						// 是目录，在限制范围内继续递归查找
						if (depth>0)depth--;
						findCSS(basePath+(basePath.length()>0?"/":"")+file.getName(), file, depth);
					}else if (file.isFile()){
						// 是文件
						if (file.getName().toLowerCase().endsWith(".css")){
							boolean exclude = false;
							for(String excludeFile:excludeCss){
								if(file.getName().toLowerCase().startsWith(excludeFile)){
									exclude = true;
									break;
								}
							}
							if(!exclude){
								String cssPath = basePath + (basePath.length()>0?"/":"") + file.getName();
								cssMap.put(cssPath.toLowerCase(), cssPath);
								selectMainCssFile(file, cssPath);
							}
						}else if (basePath.length()==0 && file.getName().toLowerCase().equalsIgnoreCase(icon)){
							// Style目录下有指定的图片文件作为该风格的标题图标
							hasIcon = true;
						}
					}
				}
			}
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private String displayName;

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public boolean isValid() {
		if (hasIcon){
			return valid;
		}else {
			return false;
		}
	}

	public String getMainCss() {
		String path = getPath();
		if (path.length()>0){
			path += "/"+mainCss;
		}
		return path;
	}

	public String getPath() {
		String path = "";
		
		if (themeType==null){
			path = "";
		}else{
			path = themeType.getPath();
		}
		if (path.length()>0){
			if (name.length()>0){
				path += "/"+name;
			}
		}else{
			if (name.length()>0){
				path += name;
			}
		}
		
		return path;
	}

	public void selectMainCssFile(File cssFile, String cssPath) {
		
		if (mainCssFile==null){
			if (cssFile!=null&&cssFile.exists()){
				mainCssFile = cssFile;
				this.mainCss = cssPath;
			}
		}else{
			if (mainCssFile.exists()&&cssFile!=null&&cssFile.exists()&&cssFile.length()>=mainCssFile.length()){
				mainCssFile = cssFile;
				this.mainCss = cssPath;
			}
		}
	}

	@Override
	public String toString() {
		return name;
	}
	
}
